package lt.marsrutai.mobile.android.map.overlay;

import com.google.android.maps.OverlayItem;

import lt.marsrutai.mobile.android.R;
import lt.marsrutai.mobile.android.widget.ConstrainedLayout;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

/**
 * View representing a balloon with a title and text snippet.
 * 
 * @author Justas
 */
public class BalloonView<T extends OverlayItem> extends FrameLayout {

    protected ConstrainedLayout layout;
    protected TextView title;
    protected TextView snippet;

    public BalloonView(Context context) {
        super(context);

        init(context);
    }
    
    public BalloonView(Context context, AttributeSet attrs) {
        super(context, attrs);

        init(context);
    }
    
    public BalloonView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        init(context);
    }

    protected void init(Context context) {
        layout = new ConstrainedLayout(context);
        layout.setVisibility(VISIBLE);

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(getLayout(), layout);
        title = (TextView) view.findViewById(R.id.title);
        snippet = (TextView) view.findViewById(R.id.snippet);

        addView(layout);
    }

    protected int getLayout() {
        return R.layout.map_balloon;
    }

    /**
     * Set the data to this balloon
     * 
     * @param item
     */
    public void setData(T item) {
        layout.setVisibility(VISIBLE);
        
        title.setText(item.getTitle());
        title.setVisibility(item.getTitle() != null ? VISIBLE : GONE);
        
        snippet.setText(item.getSnippet());
        snippet.setVisibility(item.getSnippet() != null ? VISIBLE : GONE);
    }
}
